package com.phaeton.beemeup.entity;

import java.util.Map.Entry;
import java.util.UUID;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;

import com.phaeton.beemeup.GameManager;

public class Flower extends AnimatedSprite{
	public static final int 
				FREE = 0, 
				OCCUPIED = 1, 
				RESERVED = 2, 
				DESTROYED = 4, 
				GROWING = 8,
				FIGHT = 16;
	private int state = 0;
	private int honey = 3;
	private int material = 3;
	private int health = 100;
	private Insect occupant;
	private TimerHandler honeyRegTimer;
	private TimerHandler materialRegTimer;
	private TimerHandler honeyColTimer;
	private TimerHandler materialColTimer;
	private GameManager mGameManager;
	
	public Flower(float pX, float pY, ITiledTextureRegion pTiledTextureRegion, final GameManager pGameManager) {
		super(pX, pY, pTiledTextureRegion, pGameManager.getActivity().getVertexBufferObjectManager());

		mGameManager = pGameManager;
		
		honeyColTimer = new TimerHandler(2, true, new ITimerCallback(){

			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				if(honey > 0 && occupant != null && occupant.getResource() < 3){					
					honey--;
					occupant.changeResource(1);
				}else{
					pGameManager.getLevel().unregisterUpdateHandler(pTimerHandler);
					occupant.removeState(IInsect.COLLECTING);
				}
			}
        	
        });
		
		materialColTimer = new TimerHandler(2.5f, true, new ITimerCallback(){

			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				if(material > 0 && occupant.getResource() < 3
						/*&& flower.getOccupant() != null && flower.getOccupant().equals(entry.getValue())*/){
					
					material--;
					occupant.changeResource(1);
				}else{
					pGameManager.getLevel().unregisterUpdateHandler(pTimerHandler);
					occupant.removeState(IInsect.COLLECTING);
				}
				
			}
        	
        });
		
		this.registerUpdateHandler(new IUpdateHandler(){
			@Override
			public void onUpdate(float pSecondsElapsed) {				
				//Darstellung
				if(honey == 3 && material == 3) stopAnimation(0);
				if(honey == 3 && material == 2) stopAnimation(1);
				if(honey == 3 && material == 1) stopAnimation(2);
				if(honey == 2 && material == 3) stopAnimation(3);
				if(honey == 2 && material == 2) stopAnimation(4);
				if(honey == 2 && material == 1) stopAnimation(5);
				if(honey == 1 && material == 3) stopAnimation(6);
				if(honey == 1 && material == 2) stopAnimation(7);
				if(honey == 1 && material == 1) stopAnimation(8);
				if(honey == 0 && material == 3) stopAnimation(9);
				if(honey == 0 && material == 2) stopAnimation(10);
				if(honey == 0 && material == 1) stopAnimation(11);
				if(material == 0){
					stopAnimation(12);
					addState(DESTROYED);
					honey = 0;
				}
			}
			
			@Override
			public void reset() {
			}
		});
		
		honeyRegTimer = new TimerHandler(10, true, new ITimerCallback(){
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				
				//Honig Regeneration
				if((state & (OCCUPIED | DESTROYED)) == 0){
					if(honey < 3) honey++;
				}
				
				if(honey >= 3) Flower.this.unregisterUpdateHandler(pTimerHandler);
			}        	
        });
		
		materialRegTimer = new TimerHandler(15, true, new ITimerCallback(){
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				
				//Material Regeneration
				if((state & Flower.DESTROYED) != 0){
					if(material < 3) material++;
					if(material == 3) removeState(Flower.DESTROYED);
				}

				if(material >= 3) Flower.this.unregisterUpdateHandler(pTimerHandler);
			}
        });
		
	}

	public int getHoney() {
		return honey;
	}

	public int getMaterial() {
		return material;
	}

	public void changeHoney(int i) {
		this.honey += i;
	}
	
	public void setHoney(int i) {
		this.honey = i;
	}

	public void changeMaterial(int i) {
		this.material += i;
	}
	
	public void setMaterial(int i){
		this.material = i;
	}

	public int getHealth() {
		return health;
	}
	
	public void addHealth(int i) {
		this.health += i;
	}
	
	public void addState(int state) {
		this.state |= state;
	}
	
	public void removeState(int state) {
		this.state &= ~state;
	}
	
	public int getState() {
		return state;
	}
	
	public void occupy(Insect occupant){
		this.occupant = occupant;
		this.addState(Flower.OCCUPIED);
		
		if(occupant instanceof Bee && honey > 0 && occupant.getResource() < 3){
			occupant.addState(Insect.COLLECTING);
			this.registerUpdateHandler(honeyColTimer);
		}
		if(occupant instanceof Wasp && material > 0 && occupant.getResource() < 3){
			occupant.addState(Insect.COLLECTING);
			this.registerUpdateHandler(materialColTimer);
		}		
	}
	
	public void deOccupy(){
		this.removeState(Flower.OCCUPIED);
		if(occupant != null) occupant.removeState(Insect.COLLECTING);
		
		this.unregisterUpdateHandler(honeyColTimer);
		this.unregisterUpdateHandler(materialColTimer);
		
		if(honey < 3){
			honeyRegTimer.reset();
			this.registerUpdateHandler(honeyRegTimer);
		}
		
		if(material <= 0){
			materialRegTimer.reset();
			this.registerUpdateHandler(materialRegTimer);
		}
		
		occupant = null;
	}

	public Insect getOccupant() {
		return occupant;
	}

	public void setOccupant(Insect occupant) {
		this.occupant = occupant;
	}
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		if(pSceneTouchEvent.isActionDown()){
			if(mGameManager.getSelectedBee() != null && (state & (Flower.OCCUPIED | Flower.RESERVED)) == 0){
				Bee bee = mGameManager.getBees().get(mGameManager.getSelectedBee());
				bee.setTarget(this);
				bee.fly();
				return true;
			}
		}
		return false;
	}
}
